﻿#region

using System;
using MediaSage.Lib.Configuration;
using MediaSage.Lib.Database;
using MediaSage.Lib.Plugin;
using NHibernate;
using NHibernate.Criterion;

#endregion

namespace MediaSage.Lib.Utilities
{
    public static class Plugin
    {
        #region Methods

        public static PluginEntity GetConfigurationByName(string ProviderName)
        {
            PluginEntity peReturn = null;

            try
            {
                ISession session = DBUtil.GetSession();
                ICriteria selectCriteria = session.CreateCriteria(typeof(PluginEntity));

                selectCriteria.Add(Restrictions.Eq("Name", ProviderName));
                peReturn = selectCriteria.UniqueResult<PluginEntity>();
            }
            catch (Exception)
            {
            }
            return peReturn;
        }

        public static PluginEntity GetConfigurationByNameAndVer(string ProviderName, int MajorVer, int MinorVer,
                                                                int BuildNumber)
        {
            PluginEntity peReturn = null;

            try
            {
                ISession session = DBUtil.GetSession();
                ICriteria selectCriteria = session.CreateCriteria(typeof(PluginEntity));

                selectCriteria.Add(Restrictions.Eq("Name", ProviderName));
                selectCriteria.Add(Restrictions.Eq("MajorVersion", MajorVer));
                selectCriteria.Add(Restrictions.Eq("MinorVersion", MinorVer));
                selectCriteria.Add(Restrictions.Eq("BuildNumber", BuildNumber));

                peReturn = selectCriteria.UniqueResult<PluginEntity>();
            }
            catch (Exception)
            {
            }
            return peReturn;
        }

        public static PluginBase LoadPlugin(Config config, PluginEntity PE)
        {
            return config.PluginManager.GetPlugin(PE);
        }

        #endregion Methods
    }
}